package com.coder.service;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.coder.model.DayIsa;
import com.coder.model.Isa;
import com.coder.utils.DateUtilMy;
import com.coder.utils.DecodeUtil;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration({"file:WebContent/WEB-INF/applicationContext.xml"})  
public class IsaServiceTest {

    @Resource
    private IsaService isaService;
    
	@Before
	public void setUp() throws Exception {
		System.out.println("setUp method");
	} 

	/**
	 * junit一次只运行一个测试方法的方式是到Outline视图下运行方法
	 */
	
	@Test
	public void testSaveAvgIsaOfaMonth(){
		String monthStr = "2014-11";
		isaService.saveAvgIsaOfaMonth(monthStr);
	}
	
	@Test
	public void testGetAvgIsaOfaDay(){
		
		String[] days = {"2014-11-1","2014-11-02","2014-11-03","2014-11-04","2014-11-05","2014-11-06",
				"2014-11-07","2014-11-08","2014-11-09","2014-11-10","2014-11-11","2014-11-12"
				,"2014-11-13","2014-11-14","2014-11-15","2014-11-16","2014-11-17","2014-11-18"
				,"2014-11-19","2014-11-20","2014-11-21","2014-11-22"};
		
		List<Date> dayList = new ArrayList<Date>();
		try {
			for(int i=0;i<days.length;i++)
				dayList.add(DateUtilMy.dayFormat.parse(days[i]));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		for(int i=0;i<dayList.size();i++)
			isaService.saveAvgIsaOfaDay(dayList.get(i));
	}
	
	@Test
	public void testSaveDayIsa(){
		DayIsa dayIsa = new DayIsa();
		float[] datas = {(float) 1.23,(float)2.23,(float)4.23,23};
		
		String dataStr = DecodeUtil.floats2Str(datas);
		
		dayIsa.setAvgValStr(dataStr);
		dayIsa.setMode(0);
		dayIsa.setDay(new Date());
		dayIsa.setMaxValStr(dataStr);
		dayIsa.setMinValStr(dataStr);
		dayIsa.setProdNr("pn");
		dayIsa.setSensorNr("sn");
		dayIsa.setSerNr("sern");
		
	//	dayIsa = isaService.saveDayIsa(dayIsa);
		
		System.out.println(dayIsa.getId());
		
	}
	
	@Test
	public void testGetIsaDataByDateInterval() {
		
		String dateBefore = "2014-11-16";
		String timeBefore = "05:21:00";
		
		String dateAfter = "2014-11-16";
		String timeAfter = "05:30:30";
		
		List<Isa> datas = isaService.getIsaData(dateBefore, timeBefore, dateAfter, timeAfter);
		
		Isa first = datas.get(5);
		
		for(int i=0;i<first.getFloatDatas().size();i++){
			System.out.println(first.getFloatDatas().get(i));
		}
		
		System.out.println(first.getFloatDatas().size());
		System.out.println(datas.size());
		
		findMax(first.getFloatDatas());
	}

	//寻找list中的最大值
	private void findMax(ArrayList<Float> datas){
		float max = Float.MIN_VALUE;
		int index = -1;
		for(int i=0;i<datas.size();i++){
			if(datas.get(i)>max){
				max = datas.get(i);
				index = i;
			}
		}
		
		System.out.println("max:"+max+" index:"+index);
	}
	
	
	public IsaService getIsaService() {
		return isaService;
	}

	public void setIsaService(IsaService isaService) {
		this.isaService = isaService;
	}

}
